package com.dalei.vds.soap;

import java.io.IOException;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;

import com.dalei.vdserver.shared.model.ErrorCode;
import com.dalei.vdserver.shared.soap.LoginSoap;
import com.google.gson.Gson;

public class LoginSoapImpl extends BaseSoap implements LoginSoap {

	private String url;

	private HttpTransportSE ht;

	private SoapSerializationEnvelope envelope;
	
	public LoginSoapImpl(String ipAddress, int port, int timeout) {
		// TODO Auto-generated constructor stub
		super(ipAddress, port, timeout);
		StringBuffer buffer = new StringBuffer();
		buffer.append("http://");
		buffer.append(super.getIpAddress());
		buffer.append(":");
		buffer.append(super.getPort());
		buffer.append("/VDServer/LoginService?wsdl");
		url = buffer.toString();
		ht = new HttpTransportSE(url, super.getTimeout());
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	}

	public LoginSoapImpl(Context context) {
		// TODO Auto-generated constructor stub
		super(context);
		StringBuffer buffer = new StringBuffer();
		buffer.append("http://");
		buffer.append(super.getIpAddress());
		buffer.append(":");
		buffer.append(super.getPort());
		buffer.append("/VDServer/LoginService?wsdl");
		url = buffer.toString();
		ht = new HttpTransportSE(url, super.getTimeout());
		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	}

	@SuppressWarnings("unchecked")
	public List<String> getUsers() {
		// TODO Auto-generated method stub
		List<String> users ;
		SoapObject request = new SoapObject(NAMESPACE, "getUsers");
		envelope.bodyOut = request;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			String json = result.getProperty("return").toString();
			Gson gson = new Gson();
			users = gson.fromJson(json, List.class);
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return users;
	}

	public int validateUser(String user, String password) {
		// TODO Auto-generated method stub
		SoapObject request = new SoapObject(NAMESPACE, "validateUser");
		envelope.bodyOut = request;
		request.addProperty("user", user);
		request.addProperty("password", password);
		int i = 0;
		try {
			ht.call(null, envelope);
			SoapObject result = (SoapObject) envelope.bodyIn;
			String returnObject = result.getProperty("return").toString();
			i = Integer.parseInt(returnObject);
		} catch (ClassCastException e) {
			// TODO: handle exception
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ErrorCode.INTERNAL_ERROR;
		}
		return i;
	}

}
